﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using DataContextLayer;
using BlueHarvestSnippetGenerator.TutorialClasses.TutorialModel;

namespace BlueHarvestSnippetGenerator.TutorialClasses.TutorialController
{
    public class TutorialParser
    {
        
        private readonly XDocument _tutorialDoc;
        private Tutorial _tutorial;

        public TutorialParser()
        {
            _tutorialDoc = XDocument.Load(UserProfile.CurrentTutorial,LoadOptions.SetLineInfo);
            
            _tutorial = new Tutorial();
        }

        public Tutorial CreateTutorial()
        {
            _tutorial.Steps = new List<string>();
            _tutorial.Answers = new List<string>();

            var query = from t in _tutorialDoc.Descendants("TutorialInformation")
                        select t;

            foreach (var element in query)
            {
                _tutorial.Title = element.TryGetElementValue("Title");
                _tutorial.Author = element.TryGetElementValue("Author");
                foreach (var descendant in element.Descendants("Steps"))
                {
                   _tutorial.Steps.Add(descendant.TryGetElementValue("Step"));
                   _tutorial.Answers.Add(descendant.TryGetElementValue("Answer"));
                }
            }

            return _tutorial;







        }

        

    }
}
